<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Config;//引入配置
use TCPDF;
use Elasticsearch\ClientBuilder;

class Esproducts extends Base
{   
    private $client;
    private $redis;
    private $suoyin='ceshi';
    /*private $hosts = [
        'elastic:Lily8888@149.28.44.107:9200'  
    ];
*/
    private $hosts = [
        '106.15.202.104:9200'  // SSL to IP + Port
    ];

    public function __construct(){
        parent::__construct();
        $this->client=ClientBuilder::create()->setHosts($this->hosts)->setRetries(0)->build();

        $this->redis = new \Redis();
        $this->redis->connect(config('app.redis_ip'), 6379);
        $this->redis->auth(config('app.redis_Secret'));
        $this->redis->select(3);
    }
     public function index(){
        $sql="SELECT wname FROM lily_listkey GROUP BY wname";
        $wname=Db::query($sql);

        $params['index']='esproduct2';
        $params['body']['settings']=[
            'number_of_shards' => 1
        ];
        $data['class_id']=['type' => 'long'];
        $data['pid']=['type' => 'long'];
        $data['part']=['type' => 'keyword'];
        foreach ($wname as $key => $value) {
            $data[$value['wname']]=['type'=>'keyword'];
            $data[md5($value['wname'])]=['type'=>'long'];
        }
        //dump($data);exit;
        $params['body']['mappings']['properties']=$data;
        $this->client->indices()->create($params);
    }


    public function index_upload(){
        $filename='left2';
        $load=explode('application', __DIR__);
        if(file_exists($load[0].'public/static/json/'.$filename.'.json')){
            unlink($load[0].'public/static/json/'.$filename.'.json');
        }
        $sql="SELECT wname FROM lily_listkey GROUP BY wname";
        $wname=Db::query($sql);
        $field_arr=[];
        foreach ($wname as $key => $value) {
            if(!in_array($value['wname'],$field_arr)){
                $field_arr[]=$value['wname'];
            }
        }
        $count =Db::table('lily_products')->count();
        $products = Db::table('lily_products')->field('id,class_id,part_number')->cursor();
        $i=1;
        $j=1;
        foreach ($products as $key => $value) {
            $data=[];
            $data['class_id']=$value['class_id'];
            $data['pid']=$value['id'];
            $data['part']=$value['part_number'];
            $detail=Db::table('lily_products_detail')->field('key,value')->where([['is_fen_ge','=',0],['is_pi_pei','=',0],['is_show','=',1],['sup','=',null],['url','=',null],['is_show_key','=',1],['pid','=',$value['id']]])->select();
            foreach ($detail as $k => $v) {
                if(in_array($v['key'],$field_arr)){
                    $data[$v['key']]=$v['value'];
                    if (strpos($v['value'], '"') !== false) {
                        if (strpos($v['value'], '-') !== false) {
                            $data[md5($v['key'])]='0';
                        }else{
                            $in=explode('"', $v['value']);/*分组后是数字的直接赋值，不是数字的如何区分是否是分数*/
                            if(is_numeric(trim($in[0]))){
                                $data[md5($v['key'])]=trim($in[0]);
                            }else{
                                /*分数处理*/
                                if (strpos(trim($in[0]), '/') !== false) {  /*再次区分没有斜杠的一定不是分数*/
                                    if (strpos(trim($in[0]), ' ') !== false) { /*在有斜杠的情况下，再次判断是否有空格*/
                                        $in2=explode(' ', trim($in[0]));
                                        if(is_numeric(trim($in2[0]))){
                                            $in3=explode('/', trim($in2[1]));
                                            if(is_numeric(trim($in3[0])) && is_numeric(trim($in3[1]))){ /*两个都是数字的情况*/
                                                $data[md5($v['key'])]=trim($in2[0])+trim($in3[0])/trim($in3[1]);
                                            }else{
                                                $data[md5($v['key'])]='0';
                                            }

                                        }else{
                                            $data[md5($v['key'])]='0';
                                        }
                                    }else{ /*再次分组*/
                                        $in2=explode('/', trim($in[0]));
                                        if(is_numeric(trim($in2[0])) && is_numeric(trim($in2[1]))){ /*两个都是数字的情况*/
                                            $data[md5($v['key'])]=trim($in2[0])/trim($in2[1]);
                                        }else{
                                            $data[md5($v['key'])]='0';
                                        }
                                    }
                                }else{
                                    $data[md5($v['key'])]='0';
                                }
                            }
                        }
                    }else{
                        if(strpos($v['value'], 'mm') !== false) {
                            $in=explode('mm', $v['value']);
                            if(is_numeric(trim($in[0]))){
                                $data[md5($v['key'])]=trim($in[0]);
                            }else{
                                $data[md5($v['key'])]='0';
                            }
                        }else{
                           
                            if(is_numeric(trim($v['value']))){ /*是否是数字字符串*/
                                $data[md5($v['key'])]=trim($v['value']);  
                            }else{
                                $data[md5($v['key'])]='0';
                            }
                            
                        }
                    }
                }
            }

            if($i==$count){
                $str=json_encode($data).chr(10);
            }else{
                $str=json_encode($data).chr(10);
            }
            $i++;
            $str2='{ "index" : { "_index" : "esproduct2", "_id" : "'.$j.'" } }'.chr(10);
            $j++;
 
            $myfiletxt=fopen($load[0].'public/static/json/'.$filename.'.json', "a+");
            fwrite($myfiletxt, $str2);
            fwrite($myfiletxt, $str);
            fclose($myfiletxt); 
        }

         
    }


   


}